/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');


var qdWjQuestionController = {
    getQdWjQuestionPage: function (req, res) {
        co(function () {
            var params = {};
            var paramT = {};
            if (req.query.wjId != undefined) {
                params.wjId = parseInt(req.query.wjId);
            }
            if (req.query.type != undefined) {
                params.type = parseInt(req.query.type);
            }
            if (req.query.title != undefined) {
                params.title = { "$like": "%" + req.query.title + "%" };
            }
            paramT.where = params;
            paramT.order = [["code", "ASC"]];
            var page = parseInt(req.query.page);
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            seq.qdWjQuestionModule.findAndCountAll(paramT).then(function (result) {
                res.json({
                    'success': true, 'msg': '查询成功', 'page': page, 'total': result.count, 'result': result.rows
                });
            })

        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    getWjQuestionById: function (req, res) {
        co(function () {
            var paramT = {};
            paramT.where = req.params;
            seq.qdWjQuestionModule.findOne(paramT).then(function (result) {
                res.json({ 'success': true, 'msg': '查询成功', 'result': result });
            })

        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })
    },
    addQdQuestionWj: function (req, res) {
        co(function () {
            req.body.id = util.getUUID();
            req.body.createTime = util.getNowTime();
            seq.qdWjQuestionModule.create(req.body).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '添加成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '添加失败' });
            console.log(e);
        })
    },
    updateQdQuestionWj: function (req, res) {
        co(function () {
            seq.qdWjQuestionModule.update(req.body, { where: req.params }).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '修改成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '修改失败' });
            console.log(e);
        })
    },
    deleteQdQuestionWj: function (req, res) {
        co(function () {
            var params = req.query.params;
            var arr = params.split(",");
            var paramT = [arr];
            seq.qdWjQuestionModule.destroy({ where: { id: paramT } }).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '删除成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '删除失败' });
            console.log(e);
        })
    }


}
module.exports = qdWjQuestionController;